# 编译器和编译选项
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -pthread -D_GNU_SOURCE

# 头文件目录
INCLUDES = -I.

# 头文件依赖
HEADERS = log_utils.h log_modules.h msg_queue.h gpio_lib.h uart_common.h \
          thread1.h thread2.h thread3.h thread4.h thread5.h thread6.h thread7.h \
          thread8.h thread9.h thread10.h log_service.h thread11.h thread12.h \
          proximity_sensor_common.h proximity_sensor_config.h sensor_collector.h

# 源文件
SOURCES = main.c log_utils.c msg_queue.c gpio_lib.c uart_common.c \
          thread1.c thread2.c thread3.c thread4.c thread5.c thread6.c thread7.c \
          thread8.c thread9.c thread10.c log_service.c thread11.c thread12.c \
          proximity_sensor_common.c sensor_collector.c

# 目标文件
OBJECTS = $(SOURCES:.c=.o)

# 目标程序
TARGET = multi_thread_program

# 默认目标
all: $(TARGET)

# 链接目标程序
$(TARGET): $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $^ -lrt -lm

# 编译源文件
%.o: %.c $(HEADERS)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# 清理编译生成的文件
clean:
	rm -f $(OBJECTS) $(TARGET)

# 安装（可选）
install: $(TARGET)
	cp $(TARGET) /usr/local/bin/

# 卸载（可选）
uninstall:
	rm -f /usr/local/bin/$(TARGET)

# 调试版本
debug: CFLAGS += -g -DDEBUG -DMEMORY_LEAK_DETECTION
debug: $(TARGET)

# 发布版本
release: CFLAGS += -O2 -DNDEBUG
release: $(TARGET)

# 内存泄漏检测版本
memcheck: CFLAGS += -g -DMEMORY_LEAK_DETECTION -DDEBUG
memcheck: $(TARGET)

# 显示帮助信息
help:
	@echo "可用的目标:"
	@echo "  all      - 编译程序（默认）"
	@echo "  clean    - 清理编译生成的文件"
	@echo "  debug    - 编译调试版本"
	@echo "  release  - 编译发布版本"
	@echo "  memcheck - 编译内存泄漏检测版本"
	@echo "  install  - 安装程序到系统"
	@echo "  uninstall- 从系统卸载程序"
	@echo "  help     - 显示此帮助信息"

.PHONY: all clean install uninstall debug release memcheck help 